package com.bluefish.bltuan.listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.applerao.urlrewriter.permalink.GenericPermalinkDefinition;
import com.applerao.urlrewriter.servlet.listener.URLRewriteListener;
import com.bluefish.bltuan.module.Category;
import com.bluefish.bltuan.module.cms.Content;
import com.bluefish.bltuan.permalink.ShopPermalinkDefinition;
import com.bluefish.bltuan.service.CategoryService;
import com.bluefish.bltuan.service.ContentService;

public class URLListener extends URLRewriteListener{
	
	private static final String CMS = "/cms/";
	private static final String PAST = "/past";
	private static final String INDEX = "/index/";
	private static final String QUESTION = "/question";
	private static final String TRANSFER = "/transfer";
	private static final String INFO = "/info/";
	private static final String SLASH = "/";

	@Override
	public Map<String, String> initRules() {
		Map<String, String> rules = new HashMap<String, String>();
		initBasic(rules);
		initCities(rules);
		initIntroductions(rules);
		initHelps(rules);
		initMessages(rules);
		return rules;
	}

	private void initBasic(Map<String, String> rules) {
		rules.put("/", "/index");
		rules.put("/index.jsp", "/index");
		rules.put("/index.html", "/index");
		rules.put("/past", "/index/past");
		rules.put("/question", "/info/question");
		rules.put("/transfer", "/info/transfer");
	}

	private void initIntroductions(Map<String, String> rules) {
		ContentService contentService = getBean("contentService");
		List<Content> introductions = contentService.listIntroductions();
		for(Content introduction:introductions){
			if(introduction != null){
				String nick = introduction.getNick();
				rules.put(SLASH + nick, CMS + nick);
			}
		}
	}
	
	private void initHelps(Map<String, String> rules) {
		ContentService contentService = getBean("contentService");
		List<Content> helps = contentService.listHelps();
		for(Content help:helps){
			if(help != null){
				String nick = help.getNick();
				rules.put(SLASH + nick, CMS + nick);
			}
		}
	}
	
	private void initCities(Map<String, String> rules) {
		CategoryService categoryService = getBean("categoryService");
		List<Category> cities = categoryService.listStartedCities();
		for(Category city:cities){
			if(city != null){
				rules.put(SLASH+city.getNickname(), INDEX+city.getNickname());
				rules.put(SLASH+city.getNickname()+PAST, INDEX+city.getNickname()+PAST);
			}
		}
	}
	
	private void initMessages(Map<String, String> rules) {
		CategoryService categoryService = getBean("categoryService");
		List<Category> cities = categoryService.listStartedCities();
		for(Category city:cities){
			if(city != null){
				rules.put(SLASH+city.getNickname()+QUESTION, INFO+city.getNickname()+QUESTION);
				rules.put(SLASH+city.getNickname()+TRANSFER, INFO+city.getNickname()+TRANSFER);
			}
		}
	}
	
	@Override
	public Map<GenericPermalinkDefinition, String> initPDs() {
		Map<GenericPermalinkDefinition, String> pds = new HashMap<GenericPermalinkDefinition, String>();
		ShopPermalinkDefinition definition = new ShopPermalinkDefinition("/%{tuangou_city}/deal/%{tuangou_nick}.html");
		pds.put(definition, "/shop/$2");
		return pds;
	}

}
